﻿using NCP.Model;
using NCP.Util;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using SqlSugar;
using NCP.Core.DataUtil;
using System.Data;
using NCP.Core;
using System.Configuration;

namespace WebApp
{
    public static class SelectExtensions
    {
        /// <summary>
        /// 集采批次选择
        /// </summary>
        public static MvcHtmlString RelGroupPurchaseSelect(this HtmlHelper htmlHelper, string nameStr, string val = "", string title = "请选择集采批次")
        {
            List<SelectListItem> selList = new List<SelectListItem>();
            SelectListItem item = new SelectListItem() { Value = "", Text = title };
            selList.Add(item);
            IDictionary<string, object> attrDict = new Dictionary<string, object>();
            attrDict.Add("class", "layui-input layui-select");
            attrDict.Add("lay-search", "");
            attrDict.Add("lay-filter", nameStr);
            SqlSugarClient _client = DbHelper.GetSqlSugarClient();
            List<GroupPurchase> GroupPurchaseList = _client.Queryable<GroupPurchase>().OrderBy("BeginDate desc").ToList();
            foreach (var groupPurchas in GroupPurchaseList)
            {
                item = new SelectListItem() { Value = groupPurchas.Code, Text = groupPurchas.Year + "年" + groupPurchas.Name };
                if (groupPurchas.Code == val)
                    item.Selected = true;
                selList.Add(item);
            }

            return htmlHelper.DropDownList(nameStr, selList, attrDict);
        }

        /// <summary>
        /// 集采批次选择
        /// </summary>
        public static MvcHtmlString RelDrugSelect(this HtmlHelper htmlHelper, string nameStr, string val = "", string title = "请选择药品名称")
        {
            List<SelectListItem> selList = new List<SelectListItem>();
            SelectListItem item = new SelectListItem() { Value = "", Text = title };
            selList.Add(item);
            IDictionary<string, object> attrDict = new Dictionary<string, object>();
            attrDict.Add("class", "layui-input layui-select");
            attrDict.Add("lay-search", "");
            SqlSugarClient _client = DbHelper.GetSqlSugarClient();
            List<PhaBaseInfo> drugList = _client.Queryable<PhaBaseInfo>().ToList();
            foreach (var drug in drugList)
            {
                item = new SelectListItem() { Value = drug.DrugCode, Text = drug.DrugName };
                if (drug.DrugCode == val)
                    item.Selected = true;
                selList.Add(item);
            }

            return htmlHelper.DropDownList(nameStr, selList, attrDict);
        }

        /// <summary>
        /// 科室选择
        /// </summary>
        public static MvcHtmlString DeptSelect(this HtmlHelper htmlHelper, string nameStr, string val = "", string title = "请选择科室")
        {
            List<SelectListItem> selList = new List<SelectListItem>();
            SelectListItem item = new SelectListItem() { Value = "", Text = title };
            selList.Add(item);
            IDictionary<string, object> attrDict = new Dictionary<string, object>();
            attrDict.Add("class", "layui-input layui-select");
            attrDict.Add("lay-search", "");
            SqlSugarClient _client = DbHelper.GetSqlSugarClient();
            List<DeptInfo> deptList = _client.Queryable<DeptInfo>().ToList();
            foreach (var deptinfo in deptList)
            {
                item = new SelectListItem() { Value = deptinfo.DeptCode, Text = deptinfo.DeptName };
                if (deptinfo.DeptCode == val)
                    item.Selected = true;
                selList.Add(item);
            }

            return htmlHelper.DropDownList(nameStr, selList, attrDict);
        }

        /// <summary>
        /// 科室选择
        /// </summary>
        public static MvcHtmlString DeptValidSelect(this HtmlHelper htmlHelper, string nameStr, string val = "", string title = "请选择科室")
        {
            List<SelectListItem> selList = new List<SelectListItem>();
            SelectListItem item = new SelectListItem() { Value = "", Text = title };
            selList.Add(item);
            IDictionary<string, object> attrDict = new Dictionary<string, object>();
            attrDict.Add("class", "layui-input layui-select");
            attrDict.Add("lay-search", "");
            SqlSugarClient _client = DbHelper.GetSqlSugarClient();
            List<DeptInfo> deptList = _client.Queryable<DeptInfo>().Where(x => x.ValidFlag == "1").ToList();
            foreach (var deptinfo in deptList)
            {
                item = new SelectListItem() { Value = deptinfo.DeptCode, Text = deptinfo.DeptName };
                if (deptinfo.DeptCode == val)
                    item.Selected = true;
                selList.Add(item);
            }

            return htmlHelper.DropDownList(nameStr, selList, attrDict);
        }

        /// <summary>
        /// 任务科室选择
        /// </summary>
        public static MvcHtmlString MissionDeptSelect(this HtmlHelper htmlHelper, string nameStr, string val = "",bool ispms=false, string title = "请选择科室")
        {
            List<SelectListItem> selList = new List<SelectListItem>();
            SelectListItem item = new SelectListItem() { Value = "", Text = title };
            selList.Add(item);
            IDictionary<string, object> attrDict = new Dictionary<string, object>();
            attrDict.Add("class", "layui-input layui-select");
            attrDict.Add("lay-search", "");
            SqlSugarClient _client = DbHelper.GetSqlSugarClient();
            List<UserDeptPermission> userDeptPermissions = new List<UserDeptPermission>();
            Member oper = MemberUtil.GetCurrentMember();
            if (ispms) {
                userDeptPermissions = _client.Queryable<UserDeptPermission>().Where(t => t.Code == oper.Code).ToList();
            }
            string IsAllMissionDeptOper= ConfigurationManager.AppSettings["IsAllMissionDeptOper"].ToString();//获取是否查看全部科室任务量
            string GetRoleSql = string.Format(" select e.RoleCode from MemberInRole l left join Role e on e.Id = l.RoleId where l.Code = '{0}'", oper.Code);//根据工号获取角色
            string role = _client.Ado.GetString(GetRoleSql);

            string getsql = "";
            if (IsAllMissionDeptOper.IndexOf(role) >= 0)//如果配置了该角色，则显示所有任务科室
            {
                getsql = "select distinct g.DeptCode,g.DeptName from GroupMission g";
            }
            else
            {
                if (userDeptPermissions.Count > 0)
                    getsql = string.Format("select distinct g.DeptCode,g.DeptName from GroupMission g where g.DeptCode in (select p.DeptID from UserDeptPermission p where p.Code='{0}' union select c.MergeDeptCode from DeptCompare c inner join UserDeptPermission u on c.DeptCode=u.DeptID where u.Code='{0}')", oper.Code);
            }
            var dt = _client.Ado.GetDataTable(getsql);
            foreach (DataRow dr in dt.Rows)
            {
                item = new SelectListItem() { Value = dr["DeptCode"].ToString(), Text = dr["DeptName"].ToString() };
                if (dr["DeptCode"].ToString() == val)
                    item.Selected = true;
                selList.Add(item);
            }

            return htmlHelper.DropDownList(nameStr, selList, attrDict);
        }

        /// <summary>
        /// 全院科室选择
        /// </summary>
        public static MvcHtmlString ALLDeptSelect(this HtmlHelper htmlHelper, string nameStr, string val = "", bool ispms = false, string title = "请选择科室")
        {
            List<SelectListItem> selList = new List<SelectListItem>();
            SelectListItem item = new SelectListItem() { Value = "", Text = title };
            selList.Add(item);
            IDictionary<string, object> attrDict = new Dictionary<string, object>();
            attrDict.Add("class", "layui-input layui-select");
            attrDict.Add("lay-search", "");
            SqlSugarClient _client = DbHelper.GetSqlSugarClient();
            List<UserDeptPermission> userDeptPermissions = new List<UserDeptPermission>();
            Member oper = MemberUtil.GetCurrentMember();
            if (ispms)
            {
                userDeptPermissions = _client.Queryable<UserDeptPermission>().Where(t => t.Code == oper.Code).ToList();
            }
            string getsql = "select distinct o.DeptCode,o.DeptName from DeptInfo o where o.DeptType in ('1','2')";
         
            var dt = _client.Ado.GetDataTable(getsql);
            foreach (DataRow dr in dt.Rows)
            {
                item = new SelectListItem() { Value = dr["DeptCode"].ToString(), Text = dr["DeptName"].ToString() };
                if (dr["DeptCode"].ToString() == val)
                    item.Selected = true;
                selList.Add(item);
            }

            return htmlHelper.DropDownList(nameStr, selList, attrDict);
        }

        /// <summary>
        /// 药品选择
        /// </summary>
        public static MvcHtmlString LimitDrugSelect(this HtmlHelper htmlHelper, string nameStr, string val = "", string title = "请选择药品")
        {
            List<SelectListItem> selList = new List<SelectListItem>();
            SelectListItem item = new SelectListItem() { Value = "", Text = title };
            selList.Add(item);
            IDictionary<string, object> attrDict = new Dictionary<string, object>();
            attrDict.Add("class", "layui-input layui-select");
            attrDict.Add("lay-search", "");
            SqlSugarClient _client = DbHelper.GetSqlSugarClient();
            List<PhaBaseInfo> drugList = _client.Queryable<PhaBaseInfo>()
                .Where(x => x.ValidFlag == "1")
                .Select(x => new PhaBaseInfo { DrugCode = x.DrugCode, DrugName = x.DrugName, Specs = x.Specs, UnitPrice = x.UnitPrice }).ToList();
            foreach (var drug in drugList)
            {
                item = new SelectListItem() { Value = drug.DrugCode, Text = $"{drug.DrugName}【{drug.Specs}】" };
                if (drug.DrugCode == val)
                    item.Selected = true;
                selList.Add(item);
            }

            return htmlHelper.DropDownList(nameStr, selList, attrDict);
        }

        /// <summary>
        /// 获取医生
        /// </summary>
        public static MvcHtmlString DocSelect(this HtmlHelper htmlHelper, string nameStr, string val = "", string title = "请选择科室")
        {
            List<SelectListItem> selList = new List<SelectListItem>();
            SelectListItem item = new SelectListItem() { Value = "", Text = title };
            selList.Add(item);
            IDictionary<string, object> attrDict = new Dictionary<string, object>();
            attrDict.Add("class", "layui-input layui-select");
            attrDict.Add("lay-search", "");
            SqlSugarClient _client = DbHelper.GetSqlSugarClient();
            List<Member> memberList = _client.Queryable<Member>().Where(x => x.Status == "1" && x.MemberType=="D").ToList();
            foreach (var member in memberList)
            {
                item = new SelectListItem() { Value = member.Code, Text = member.Name };
                if (member.Code == val)
                    item.Selected = true;
                selList.Add(item);
            }

            return htmlHelper.DropDownList(nameStr, selList, attrDict);
        }
    }
}